<?php
/**
 * Template Name: Page Service
 * 
 */
get_header(); ?>

	<section class="main">
		<nav class="main-nav">
			<ul>						
				<?php 
					$taxonomies = 'building-type';
					$args  = array('hide_empty'=>false);
					$terms = get_terms( $taxonomies, $args ) ;
					foreach($terms as $term){							
						if( $term->parent == 0 && get_tax_meta($term->term_id,'building_tax_show_home') == 1){
							?>
						<li <?php echo checkActiveMenuHome($term->term_id)?>><a href="<?php echo get_term_link($term,$taxonomies);?>"><?php echo $term->name;?></a></li>
						<li class="line-nav"></li>
					<?php
						}
					}
				?>
				<?php $pageid = 58 ;if(ICL_LANGUAGE_CODE == 'vi') $pageid = 76 ;?>
			   <li><a href="<?php echo get_page_link($pageid)?>">
					<?php echo get_page($pageid)->post_title; ?>
				</a></li>
			</ul>
		</nav>
    	<?php get_sidebar(); ?>        
        <!-- main content -->
        <section class="main-content">
			<section class="discrumble">
				<?php if(function_exists('bcn_display')){ bcn_display();}?>
			</section>			
			<section class="col-main">
				<?php do_action('toolbar_order');?>
				<ul class="item-list">
					<?php				
						global $wp_query;
						$arr_order = array();
						if(isset($_GET['orderby'])){		
							if( $_GET['orderby'] == 'price' ){
								$orderby = 'price';				
								if(isset($_GET['order'])){	$order = 'ASC'; }
								$arr_order =array( 'orderby' => 'meta_value' , 'meta_query'=>array( 'meta_key' => 'building_price' ) , 'order'	=> $order );
							}else{
								if(isset($_GET['order'])){	$order = 'ASC'; }
								if( $_GET['orderby'] == 'title' || $_GET['orderby'] == 'date' || $_GET['orderby'] == 'modified'){
									$orderby = $_GET['orderby'];
								}
								$arr_order = array( 'order'	=> $order , 'orderby' => $orderby);
							}
						}else{
							$arr_order = array( 'order'	=> $order , 'orderby' => $orderby);
						}	
						$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
						$args = array(							
							'post_type'				=> 'building',
							'post_status'			=> 'publish',
							'ignore_sticky_posts'	=> 1,
							'posts_per_page'		=> get_option('posts_per_page'),
							'paged'            		=> $paged,			
						);	
						$args = array_merge( $arr_order ,$args );						
						$wp_query = new WP_Query( $args );	
						if ( $wp_query->have_posts() ) :?>								
								<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>		
									<li class="item">
										<h4 class="item-image">
											<a href="<?php the_permalink();?>" title="<?php the_title();?>">									
												<?php returnImageFirst($post);?>
											</a>
										</h4>
										<div class="item-right">
											<h3 class="item-name">
												<a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a>
											</h3>	
											<p>
												<strong><?php _e('Type:','building')?></strong>
												<?php echo get_the_term_list( $post->ID, 'building-type', '', ', ', '' ); ?>
											</p>
											<p>									
												<strong><?php _e('Agent Name:','building')?></strong>
												<?php j_get_author();?>
											</p>
											<p>
												<strong><?php _e('Interior:','building')?></strong>
												<?php echo rwmb_meta('building_interior');?>
												
											</p>
											<p>
												<strong><?php _e('Price:','building')?></strong>
												<?php
													$currency = get_post_meta(get_the_ID() , 'building_currency' , true);
													$arr_currency = explode('/',$currency);
													$extra = '';
													$isForSale = get_post_meta(get_the_ID() ,'building_type', false);
													foreach( $isForSale as $items){
														if( $items == 'For Rent'){
															$extra = '/'.$arr_currency[1];
														}
													}
												?>
												<span class="price-list">$<?php echo get_post_meta(get_the_ID() , 'building_cost' , true) . $extra ;?></span>									
											</p>
											<p>
												<strong><?php _e('Currency:','building')?></strong>
												<?php
													if(empty($extra)){
														echo $arr_currency[0];
													}else{
														echo $currency;
													}
												?>
											</p>								
											<p>
												<strong><?php _e('Available:','building')?></strong>
												<?php echo rwmb_meta('building_available');?>																
											</p>
										</div>
									</li>	
								<?php endwhile;?>							
						<?php endif;
						wp_reset_postdata();
						if(function_exists('wp_pagenavi')){ wp_pagenavi(); }
						
					?>
				</ul>
			</section>
        </section>
        <!-- end main content -->
        <div class="clear"></div>
    </section>
    <!-- content -->
<?php get_footer(); ?>